package com.black.consumer.controller;

import com.black.api.service.AnnotationService;
import com.black.api.service.AsyncService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.service.EchoService;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Description
 * @Author
 * @Date 2021/1/19
 * @Time 16:28
 */
@RestController
public class ConsumerController {


    /**
     * @Reference 引用的版本号必须与暴露服务的版本号一致。
     *
     */
    @Reference(version = "0.0.1")
    private AnnotationService annotationService;

    @GetMapping("/hello")
    public String consumer(){
        GenericService barService = null;
        barService.$invoke(null, null, null);
        return annotationService.sayHello("keyuefei");
    }


    /**
     * 通过回声测试检测 Dubbo 服务是否可用
     * @return
     */
    @GetMapping("/echo")
    public Object echo(){
        EchoService echoService = (EchoService) annotationService;
        return echoService.$echo("ok");
    }


    @Reference
    AsyncService asyncService;

    @GetMapping("/async")
    public Object async() throws InterruptedException, ExecutionException {
        CompletableFuture future = asyncService.sayHello("keyuefei");
        //do something else
        Thread.sleep(3000);
        return future.get();
    }




}